package com.example.fragmentmanager;

import android.content.Context;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import com.example.fragmentmanager.callback.IFragmentCallBack;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link BlankFragment1#newInstance} factory method to
 * create an instance of this fragment.
 */
public class BlankFragment1 extends Fragment {

    private View rootView;

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public BlankFragment1() {
        // Required empty public constructor
    }

    private IFragmentCallBack iFragmentCallBack;

    public void setiFragmentCallBack(IFragmentCallBack iFragmentCallBack) {
        this.iFragmentCallBack = iFragmentCallBack;
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment BlankFragment1.
     */
    // TODO: Rename and change types and number of parameters
    public static BlankFragment1 newInstance(String param1, String param2) {
        BlankFragment1 fragment = new BlankFragment1();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onAttach(@NonNull  Context context) {
        super.onAttach(context);

        Log.d("fragment","onAttach:");
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d("fragment","onCreate:");

        //1.使用Bundle实现activity与fragment之间通信
        Bundle bundle=this.getArguments();
        String msg = (String) bundle.get("msg");
        Log.i("msg","onCreate"+msg);

        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
       // return inflater.inflate(R.layout.fragment_blank2, container, false);

        Log.d("fragment","onCreateView:");

        if (rootView==null){
            rootView=inflater.inflate(R.layout.fragment_blank1,container,false);
        }
        Button button=rootView.findViewById(R.id.btn_3);
        button.setOnClickListener((view)->{
            iFragmentCallBack.sendMagToActivity("hello i am from fragment");
            String msg=iFragmentCallBack.getMsgFromActivity("");
            Toast.makeText(BlankFragment1.this.getContext(),msg,Toast.LENGTH_SHORT).show();
        });
        return rootView;

    }

    @Override
    public void onActivityCreated(@Nullable  Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Log.d("fragment","onActivityCreated:");
    }

    @Override
    public void onStart() {
        super.onStart();
        Log.d("fragment","onStart:");
    }

    /**
     * 重新获取数据
     */
    @Override
    public void onResume() {
        super.onResume();
        Log.d("fragment","onResume:");
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.d("fragment","onPause:");
    }

    /**
     * 停止
     */
    @Override
    public void onStop() {
        super.onStop();
        Log.d("fragment","onStop:");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Log.d("fragment","onDestroyView:");
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d("fragment","onDestroy:");
    }

    @Override
    public void onDetach() {
        super.onDetach();
        Log.d("fragment","onDetach:");
    }
}